package cn.tedu.boot1_2.controller;

import cn.tedu.boot1_2.entity.User;
import cn.tedu.boot1_2.utils.DBUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@RestController //相当于在Controller 注解基础上， 给所有方法都添加 @ResponseBody注解（@ResponseBody 使返回值返回成功）
public class UserController {
    @RequestMapping("/reg")
    public  String reg(User user){
        System.out.println("user = " + user);
        //获取数据库连接
        try( Connection  conn = DBUtils.getConn()) {
           String sql = "insert  into user values(null,?,?,?)";
            PreparedStatement  ps  = conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNick());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return "注册成功";
    }
    @RequestMapping("/login")
    public  String login(User  user){
        System.out.println("user = " + user);
        try (Connection  conn = DBUtils.getConn()){
            String sql = "select  password from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            //替换？为用户名
            ps.setString(1, user.getUsername());
            ResultSet  rs =ps.executeQuery();
            //判断是否查到了内容
            if (rs.next()){
                String pw = rs.getString(1);
                //验证查到的密码和登陆的密码是否一样
                if (pw.equals(user.getPassword())){
                    return "登陆成功";
                }else{
                    return "密码错误";
                }
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "用户名不存在";
    }
}
